package com.ruoyi.resume.service.impl;

import com.ruoyi.resume.domain.model.Tag;
import com.ruoyi.resume.domain.request.LabelingRequest;
import com.ruoyi.resume.exception.InvalidParameterException;
import com.ruoyi.resume.service.BasicInfoLabelingService;
import com.ruoyi.resume.utils.LabelingUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BasicInfoLabelingServiceImpl implements BasicInfoLabelingService {

    @Autowired
    private LabelingUtils labelingUtils;

    @Override
    public List<Map<String, Integer>> generateGenderLabel(LabelingRequest request) {
        validateRequest(request, "性别信息缺失");

        String gender = request.getSplitResumeResult().getBasicInfo().getGender();
        List<Map<String, Integer>> result = new ArrayList<>();
        List<Tag> tags = labelingUtils.getTagsByCategoryId(request.getCategory().getId());

        if ("M".equals(gender)) {
            addTagToResult(result, tags, "男");
        } else if ("F".equals(gender)) {
            addTagToResult(result, tags, "女");
        }
        return result;
    }

    @Override
    public List<Map<String, Integer>> generateAgeLabel(LabelingRequest request) {
        validateRequest(request, "年龄信息缺失");

        int age = request.getSplitResumeResult().getBasicInfo().getAge();
        List<Map<String, Integer>> result = new ArrayList<>();
        List<Tag> tags = labelingUtils.getTagsByCategoryId(request.getCategory().getId());

        if (age <= 25) {
            addTagToResult(result, tags, "25岁以下");
        } else if (age >= 26 && age <= 30) {
            addTagToResult(result, tags, "26-30岁");
        } else if (age >= 31 && age <= 35) {
            addTagToResult(result, tags, "31-35岁");
        } else if (age >= 36) {
            addTagToResult(result, tags, "36岁以上");
        }
        return result;
    }

    @Override
    public List<Map<String, Integer>> generatePoliticalStatusLabel(LabelingRequest request) {
        validateRequest(request, "政治面貌信息缺失");

        String politicalStatus = request.getSplitResumeResult().getBasicInfo().getPoliticalStatus();
        List<Map<String, Integer>> result = new ArrayList<>();
        List<Tag> tags = labelingUtils.getTagsByCategoryId(request.getCategory().getId());

        if (politicalStatus.contains("中共党员")) {
            addTagToResult(result, tags, "中共党员");
        } else if (politicalStatus.contains("团员")) {
            addTagToResult(result, tags, "共青团员");
        } else if (politicalStatus.contains("民主党派")) {
            addTagToResult(result, tags, "民主党派");
        } else if ("无党派".equals(politicalStatus) || "无".equals(politicalStatus)) {
            addTagToResult(result, tags, "无党派");
        }
        return result;
    }

    @Override
    public List<Map<String, Integer>> generateHometownLabel(LabelingRequest request) {
        validateRequest(request, "居住地信息缺失");

        String residence = request.getSplitResumeResult().getBasicInfo().getResidence();
        List<Map<String, Integer>> result = new ArrayList<>();
        List<Tag> tags = labelingUtils.getTagsByCategoryId(request.getCategory().getId());

        // 福建省内的市区
        List<String> citiesInFujian = Arrays.asList("福州", "厦门", "泉州", "漳州", "龙岩", "宁德", "莆田", "三明", "南平");

        // 判断是否为福建省内的市区
        boolean isInFujian = residence.contains("福建") || citiesInFujian.stream().anyMatch(residence::contains);

        if (isInFujian) {
            addTagToResult(result, tags, "省内");
        } else {
            addTagToResult(result, tags, "省外");
        }
        return result;
    }

    @Override
    public List<Map<String, Integer>> generateEthnicityLabel(LabelingRequest request) {
        validateRequest(request, "民族信息缺失");

        String ethnicity = request.getSplitResumeResult().getBasicInfo().getNation();
        List<Map<String, Integer>> result = new ArrayList<>();
        List<Tag> tags = labelingUtils.getTagsByCategoryId(request.getCategory().getId());

        if ("汉族".equals(ethnicity) || "汉".equals(ethnicity)) {
            addTagToResult(result, tags, "汉族");
        } else {
            addTagToResult(result, tags, "少数民族");
        }
        return result;
    }

    private void validateRequest(LabelingRequest request, String errorMessage) {
        if (request.getSplitResumeResult().getBasicInfo() == null) {
            throw new InvalidParameterException(errorMessage);
        }
    }

    private void addTagToResult(List<Map<String, Integer>> result, List<Tag> tags, String label) {
        for (Tag tag : tags) {
            if (label.equals(tag.getTagValue())) {
                Map<String, Integer> map = new HashMap<>();
                map.put(tag.getTagValue(), tag.getId());
                result.add(map);
                break;
            }
        }
    }
}
